package com.lvtongka.app.base

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.lvtongka.app.dialog.LoadingDialog
import com.lvtongka.app.ui.webview.WebViewActivity

abstract class BaseFragment<T : ViewBinding, VM : BaseViewModel> : Fragment() {
    private val minCLickDelayTime: Long = 500 // 时间阈值，单位毫秒
    private var lastClickTime: Long = 0
    lateinit var binding: T
    lateinit var viewModel: VM
    private var loadingDialog: LoadingDialog? = null
    // 控制状态栏颜色
    var isBlack: Boolean = false

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        viewModel = initViewModel()
        binding = initBinding(inflater, container)
        loadingDialog = context?.let { LoadingDialog(it) }
        initEvent()
        initData()
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setStatusBarFullTransparent(isBlack)
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            setStatusBarFullTransparent(false)
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            setStatusBarFullTransparent(false)
        }
    }

    open fun setStatusBarFullTransparent(isBlack: Boolean = true) {
        if (activity == null)
            return
        if (Build.VERSION.SDK_INT >= 21) {
            val window = activity!!.window
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            if (isBlack) {
                window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
            } else {
                window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
            }
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.TRANSPARENT
        } else if (Build.VERSION.SDK_INT >= 19) {
            activity!!.window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
    }

    protected abstract fun initBinding(inflater: LayoutInflater, container: ViewGroup?): T

    protected abstract fun initViewModel(): VM

    @SuppressLint("ShowToast")
    open fun showToast(msg: String?) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
    }

    open fun showLoading(msg: String?) {
        loadingDialog?.let {
            if (it.isShowing) {
                return
            }
            msg?.let {result->
                it.setMessage(result)
            }
            it.setCancelable(false)
            it.setCanceledOnTouchOutside(false)
            it.show()
        }
    }

    open fun showLoading() {
        showLoading(null)
    }

    open fun hideLoading() {
        loadingDialog?.let {
            if (it.isShowing) {
                it.dismiss()
            }
        }
    }

    protected abstract fun initEvent()

    protected abstract fun initData()

    override fun onDestroyView() {
        loadingDialog?.dismiss()
        loadingDialog = null
        super.onDestroyView()
    }

    // 快速点击
    protected fun isFastClick(): Boolean {
        var flag = false
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastClickTime >= minCLickDelayTime) {
            flag = true
        }
        lastClickTime = currentTime
        return !flag
    }

    protected fun jumpWeb(url: String, isWhite: Boolean = false) {
        val intent = Intent(context, WebViewActivity::class.java)
        intent.putExtra(WebViewActivity.URL_KEY, url)
        intent.putExtra(WebViewActivity.IS_WITHE, isWhite)
        startActivity(intent)
    }
}